<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Open Internet or Unix domain socket connection</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.dns-get-record.html">dns_get_record</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.gethostbyaddr.html">gethostbyaddr</a></div>
 <div class="up"><a href="ref.network.html">Network 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.fsockopen" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">fsockopen</h1>
  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">fsockopen</span> &mdash; <span class="dc-title">Open Internet or Unix domain socket connection</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.fsockopen-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">resource</span> <span class="methodname"><strong>fsockopen</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$hostname</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$port</code><span class="initializer"> = -1</span></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter reference">&$errno</code></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter reference">&$errstr</code></span>
   [, <span class="methodparam"><span class="type">float</span> <code class="parameter">$timeout</code><span class="initializer"> = ini_get(&quot;default_socket_timeout&quot;)</span></span>
  ]]]] )</div>

  <p class="para rdfs-comment">
   Initiates a socket connection to the resource specified by
   <em><code class="parameter">hostname</code></em>.
  </p>
  <p class="para">
   PHP supports targets in the Internet and Unix domains as described in
   <a href="transports.html" class="xref">所支持的套接字传输器（Socket Transports）列表</a>.  A list of supported transports can also be
   retrieved using <span class="function"><a href="function.stream-get-transports.html" class="function">stream_get_transports()</a></span>.
  </p>
  <p class="para">
   The socket will by default be opened in blocking mode.  You can
   switch it to non-blocking mode by using
   <span class="function"><a href="function.stream-set-blocking.html" class="function">stream_set_blocking()</a></span>.
  </p>
  <p class="para">
   The function <span class="function"><a href="function.stream-socket-client.html" class="function">stream_socket_client()</a></span> is similar but
   provides a richer set of options, including non-blocking connection and the
   ability to provide a stream context.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.fsockopen-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">hostname</code></em></dt>

     <dd>

      <p class="para">
       If OpenSSL support <a href="openssl.installation.html" class="link">is
       installed</a>, you may prefix the <em><code class="parameter">hostname</code></em>
       with either <em>ssl://</em> or <em>tls://</em> to
       use an SSL or TLS client connection over TCP/IP to connect to the
       remote host.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">port</code></em></dt>

     <dd>

      <p class="para">
       The port number. This can be omitted and skipped with
       <em>-1</em> for transports that do not use ports, such as
       <em>unix://</em>.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">errno</code></em></dt>

     <dd>

      <p class="para">
       If provided, holds the system level error number that occurred in the
       system-level <em>connect()</em> call.
      </p>
      <p class="para">
       If the value returned in <em><code class="parameter">errno</code></em> is
       <em>0</em> and the function returned <strong><code>FALSE</code></strong>, it is an
       indication that the error occurred before the 
       <em>connect()</em> call. This is most likely due to a
       problem initializing the socket.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">errstr</code></em></dt>

     <dd>

      <p class="para">
       The error message as a string.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">timeout</code></em></dt>

     <dd>

      <p class="para">
       The connection timeout, in seconds.
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="para">
        If you need to set a timeout for reading/writing data over the
        socket, use <span class="function"><a href="function.stream-set-timeout.html" class="function">stream_set_timeout()</a></span>, as the 
        <em><code class="parameter">timeout</code></em> parameter to
        <span class="function"><strong>fsockopen()</strong></span> only applies while connecting the
        socket.
       </p>
      </p></blockquote>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.fsockopen-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   <span class="function"><strong>fsockopen()</strong></span> returns a file pointer which may be used
   together with the other file functions (such as
   <span class="function"><a href="function.fgets.html" class="function">fgets()</a></span>, <span class="function"><a href="function.fgetss.html" class="function">fgetss()</a></span>,
   <span class="function"><a href="function.fwrite.html" class="function">fwrite()</a></span>, <span class="function"><a href="function.fclose.html" class="function">fclose()</a></span>, and
   <span class="function"><a href="function.feof.html" class="function">feof()</a></span>). If the call fails, it will return <strong><code>FALSE</code></strong>
  </p>
 </div>

 <div class="refsect1 errors" id="refsect1-function.fsockopen-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   Throws <strong><code>E_WARNING</code></strong> if <em><code class="parameter">hostname</code></em> is
   not a valid domain.
  </p>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.fsockopen-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>4.3.0</td>
       <td>
        Added support for the <em><code class="parameter">timeout</code></em> parameter on
        win32.
       </td>
      </tr>

      <tr>
       <td>4.3.0</td>
       <td>
        SSL and TLS over TCP/IP support was added.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.fsockopen-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4380">
    <p><strong>Example #1 <span class="function"><strong>fsockopen()</strong></span> Example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fsockopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"www.example.com"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">80</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$errno</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$errstr</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">30</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$fp</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$errstr</span><span style="color: #DD0000">&nbsp;(</span><span style="color: #0000BB">$errno</span><span style="color: #DD0000">)&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$out&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"GET&nbsp;/&nbsp;HTTP/1.1\r\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$out&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"Host:&nbsp;www.example.com\r\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$out&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"Connection:&nbsp;Close\r\n\r\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$out</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(!</span><span style="color: #0000BB">feof</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">fgets</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">128</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4381">
    <p><strong>Example #2 Using UDP connection</strong></p>
    <div class="example-contents"><p>
     The example below shows how to retrieve the day and time from the UDP
     service &quot;daytime&quot; (port 13) in your own machine.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fsockopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"udp://127.0.0.1"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">13</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$errno</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$errstr</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$fp</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"ERROR:&nbsp;</span><span style="color: #0000BB">$errno</span><span style="color: #DD0000">&nbsp;-&nbsp;</span><span style="color: #0000BB">$errstr</span><span style="color: #DD0000">&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">26</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>

 <div class="refsect1 notes" id="refsect1-function.fsockopen-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Depending on the environment, the Unix domain or the optional
    connect timeout may not be available.
   </p>
  </p></blockquote>
  <div class="warning"><strong class="warning">Warning</strong>
   <p class="para">
    UDP sockets will sometimes appear to have opened without an error,
    even if the remote host is unreachable.  The error will only
    become apparent when you read or write data to/from the socket.
    The reason for this is because UDP is a &quot;connectionless&quot; protocol,
    which means that the operating system does not try to establish
    a link for the socket until it actually needs to send or receive data.
   </p>
  </div>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">当指定数值型的 IPv6
地址（例如 <em>fe80::1</em>）时必须用方括号将 IP 围起来——例如，
<em>tcp://[fe80::1]:80</em>。</span></p></blockquote>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.fsockopen-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.pfsockopen.html" class="function" rel="rdfs-seeAlso">pfsockopen()</a> - Open persistent Internet or Unix domain socket connection</span></li>
    <li class="member"><span class="function"><a href="function.stream-socket-client.html" class="function" rel="rdfs-seeAlso">stream_socket_client()</a> - Open Internet or Unix domain socket connection</span></li>
    <li class="member"><span class="function"><a href="function.stream-set-blocking.html" class="function" rel="rdfs-seeAlso">stream_set_blocking()</a> - Set blocking/non-blocking mode on a stream</span></li>
    <li class="member"><span class="function"><a href="function.stream-set-timeout.html" class="function" rel="rdfs-seeAlso">stream_set_timeout()</a> - Set timeout period on a stream</span></li>
    <li class="member"><span class="function"><a href="function.fgets.html" class="function" rel="rdfs-seeAlso">fgets()</a> - 从文件指针中读取一行</span></li>
    <li class="member"><span class="function"><a href="function.fgetss.html" class="function" rel="rdfs-seeAlso">fgetss()</a> - 从文件指针中读取一行并过滤掉 HTML 标记</span></li>
    <li class="member"><span class="function"><a href="function.fwrite.html" class="function" rel="rdfs-seeAlso">fwrite()</a> - 写入文件（可安全用于二进制文件）</span></li>
    <li class="member"><span class="function"><a href="function.fclose.html" class="function" rel="rdfs-seeAlso">fclose()</a> - 关闭一个已打开的文件指针</span></li>
    <li class="member"><span class="function"><a href="function.feof.html" class="function" rel="rdfs-seeAlso">feof()</a> - 测试文件指针是否到了文件结束的位置</span></li>
    <li class="member"><span class="function"><a href="function.socket-connect.html" class="function" rel="rdfs-seeAlso">socket_connect()</a> - Initiates a connection on a socket</span></li>
    <li class="member">The <a href="ref.curl.html" class="link">Curl extension</a></li>
   </ul>
  </p>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.dns-get-record.html">dns_get_record</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.gethostbyaddr.html">gethostbyaddr</a></div>
 <div class="up"><a href="ref.network.html">Network 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
